package com.yyh1;

import java.util.Objects;

/*知识点：Object类的方法
案例：
	1、声明三角形类，包含a,b,c三边
	（1）属性私有化，提供无参，有参构造，提供get/set
	（2）重写：toString()
	（3）重写：hashCode和equals方法
	（4）编写  public double getArea()：求面积方法
	（5）编写 public double getPiremeter()：求周长方法
	2、声明测试类Test12，在测试类中创建两个三角形对象，调用以上方法进行测试
*/
public class Test4 {
    public static void main(String[] args) {
        //创建三角形对象
        Triangle t1 = new Triangle(3,4,5);
        //输出
        System.out.println(t1);
        System.out.println(t1.getArea());
        System.out.println(t1.getPiremeter());
        System.out.println(t1.hashCode());
        System.out.println(t1.equals(t1));
    }
}
//声明三角形类
class Triangle{
    //属性a,b,c
    private double a;
    private double b;
    private double c;
    //无参构造
    public Triangle(){

    }
    //有参构造
    public Triangle(double a,double b,double c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
    //get/set
    public double getA() {
        return a;
    }
    public void setA(double a) {
        this.a = a;
    }
    public double getB() {
        return b;
    }
    public void setB(double b) {
        this.b = b;
    }
    public double getC() {
        return c;
    }
    public void setC(double c) {
        this.c = c;
    }
    //重写toString()
    @Override
    public String toString() {
        return "Triangle{" +
                "a=" + a +
                ", b=" + b +
                ", c=" + c +
                '}';
    }
    //重写hashCode和equals方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Triangle triangle = (Triangle) o;
        return Double.compare(triangle.a, a) == 0 &&
                Double.compare(triangle.b, b) == 0 &&
                Double.compare(triangle.c, c) == 0;
    }
    @Override
    public int hashCode() {
        return Objects.hash(a, b, c);
    }
    //编写  public double getArea()：求面积方法
    public double getArea(){
        double p = (a+b+c)/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
    //编写 public double getPiremeter()：求周长方法
    public double getPiremeter(){
        return a+b+c;
    }
}
